package com.linxiao.hrcrm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.linxiao.hrcrm.vo.AllocationRequest;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.linxiao.hrcrm.domain.CrmCustomer;
import com.linxiao.hrcrm.service.ICrmCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户信息Controller
 *
 * @author zhoudong
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/hrcrm/customer")
public class CrmCustomerController extends BaseController {
    @Autowired
    private ICrmCustomerService crmCustomerService;

    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmCustomer crmCustomer) {
        startPage();
        List<CrmCustomer> list = crmCustomerService.selectCrmCustomerList(crmCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:export')")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmCustomer crmCustomer) {
        List<CrmCustomer> list = crmCustomerService.selectCrmCustomerList(crmCustomer);
        ExcelUtil<CrmCustomer> util = new ExcelUtil<CrmCustomer>(CrmCustomer.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 数据导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "客户信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<CrmCustomer> util = new ExcelUtil<>(CrmCustomer.class);
        List<CrmCustomer> customerList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        LoginUser loginUser = getLoginUser();
        String message = crmCustomerService.customerList(customerList, updateSupport, operName, loginUser.getUser().getMchId());
        return success(message);
    }


    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(crmCustomerService.selectCrmCustomerById(id));
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:query')")
    @GetMapping(value = "/getCustomerVO")
    public AjaxResult getCustomerVO(@RequestParam Long customerId,
                                    @RequestParam Long mchId,
                                    @RequestParam(required = false) Long employeeId) {
        return success(crmCustomerService.getCustomerVO(customerId, mchId, employeeId));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:add')")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmCustomer crmCustomer) {
        return toAjax(crmCustomerService.insertCrmCustomer(crmCustomer));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:allocationCustomer')")
    @Log(title = "分配客户", businessType = BusinessType.INSERT)
    @PostMapping("/allocationCustomer")
    public AjaxResult allocationCustomer(@RequestBody AllocationRequest allocationRequest) {
        return toAjax(crmCustomerService.allocationCustomer(allocationRequest));
    }

    /**
     * 获取商户下可分配的客户总数
     */
    @Log(title = "分配客户", businessType = BusinessType.INSERT)
    @GetMapping("toBeAllocationCount")
    public AjaxResult toBeAllocationCount(Long mchId) {
        return AjaxResult.success(crmCustomerService.toBeAllocationCount(mchId));
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:edit')")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmCustomer crmCustomer) {
        return toAjax(crmCustomerService.updateCrmCustomer(crmCustomer));
    }

    /**
     * 删除客户信息
     */
    @PreAuthorize("@ss.hasPermi('hrcrm:customer:remove')")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(crmCustomerService.deleteCrmCustomerByIds(ids));
    }

}
